﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using OCR.TesseractWrapper;
using System.IO;

namespace OCR
{
    public class OCRReader
    {
        public string Recognize(string imageFile)
        {
            string result = string.Empty;
            string language = "eng";
            int oem = (int)eOcrEngineMode.OEM_DEFAULT;

            using (TesseractProcessor processor = new TesseractProcessor())
            {
                using (Bitmap bmp = Bitmap.FromFile(imageFile) as Bitmap)
                {
                    string data = string.Concat(Directory.GetCurrentDirectory(), @"\Tessdata\");

                    processor.Init(data, language, oem);
                    string text = string.Empty;
                    unsafe
                    {
                        text = processor.Recognize(bmp);
                    }
                    result = text.Trim();
                }
            }
            return result;
        }

        public string RecognizeWithMODI(string imagefile)
        {
            string result = string.Empty;
            try
            {
                MODI.Document md = new MODI.Document();
                md.Create(imagefile);
                md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
                MODI.Image image = (MODI.Image)md.Images[0];
                result = image.Layout.Text;
                md.Close(false);
            }
            catch (Exception)
            {
                //TODO log exception
            }
            return result;
        }
    }
}
